﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

using ZedGraph;

namespace UCTT.NetFx
{
    public partial class Form4 : Form
    {
        #region    字段 start
        private readonly Color[] colors = new Color[] { Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Blue, Color.Indigo, Color.Purple, Color.Cyan };
        private List<int> usedIndex = new List<int>();
        private Dictionary<Color, LineItem> curveDic = new Dictionary<Color, LineItem>();

        private int num = 8;
        private int curveIndex = 0;
        private IPointListEdit[] pointListEdit;
        private LineItem[] curveList;
        #endregion 字段 end

        #region    属性 start
        /// <summary>
        /// 下一条曲线的索引
        /// </summary>
        public int NextCurveIndex
        {
            get
            {
                if (curveIndex >= num)
                    curveIndex = 0;

                return curveIndex++;
            }
        }
        #endregion 属性 end


        public Form4()
        {
            InitializeComponent();
            InitPlot();
        }

        #region    私有方法 start
        private void InitPlot()
        {
            MasterPane masterPane = zgc1.MasterPane;

            masterPane.PaneList.RemoveAt(0);
            // Set the master pane title
            //masterPane.Title.Text = "MasterPane Test";
            //masterPane.Title.IsVisible = true;

            // Fill the pane background with a color gradient
            masterPane.Fill = new Fill(Color.White, Color.MediumSlateBlue, 45.0F);

            // Set the margins and the space between panes to 10 points
            masterPane.Margin.All = 2;
            masterPane.InnerPaneGap = 2;

            // Enable the master pane legend
            //masterPane.Legend.IsVisible = true;
            //masterPane.Legend.Position = LegendPos.TopCenter;

            // Create a new GraphPane
            GraphPane myPane = new GraphPane();
            //myPane.Title.Text = "My Test Graph #1";
            //myPane.XAxis.Title.Text = "X Axis";
            //myPane.YAxis.Title.Text = "Y Axis";
            myPane.Legend.IsVisible = false;
            // Fill the pane background with a color gradient
            myPane.Fill = new Fill(Color.White, Color.LightYellow, 45.0F);
            // Reduce the base dimension to 6 inches, since these panes tend to be smaller
            myPane.BaseDimension = 6.0F;

            //zedGraphList = new IPointListEdit[ParaSoftware.ChannelTotalNumber];
            //curve = new LineItem[ParaSoftware.ChannelTotalNumber];
            //for (int i = 0; i < ParaSoftware.ChannelTotalNumber; i++)
            //{
            //    //增加series
            //    curve[i] = myPane.AddCurve(ParaSoftware.stPara1.strChanelName[i], zedGraphList[i], GND_color.arrChartSeriesColor[i], SymbolType.None);
            //    curve[i].Line.Width = ParaSoftware.ChartLineWidth;//设置线宽
            //    //绑定series数据
            //    zedGraphList[i] = curve[i].Points as IPointListEdit;
            //}

            ColorSymbolRotator colorSymbolRotator = new ColorSymbolRotator();

            pointListEdit = new IPointListEdit[num];
            curveList = new LineItem[num];
            for (int i = 0; i < num; i++)
            {
                curveList[i] = myPane.AddCurve($"label{i}", pointListEdit[i], colorSymbolRotator.NextColor, colorSymbolRotator.NextSymbol);
                pointListEdit[i] = curveList[i].Points as IPointListEdit;
            }

            // Add the new GraphPane to the MasterPane
            masterPane.Add(myPane);
            //masterPane.PaneList[]
            // Tell ZedGraph to auto layout all the panes
            using (Graphics g = zgc1.CreateGraphics())
            {
                masterPane.SetLayout(g, PaneLayout.SingleColumn);
                masterPane.AxisChange(g);
            }
        }

        private string XAxis_ScaleFormatEvent(GraphPane pane, Axis axis, double val, int index)
        {
            Debug.WriteLine($"val={val}");
            return val.ToString();
        }
        #endregion 私有方法 end

        #region    事件处理 start
        private void Form4_Load(object sender, EventArgs e)
        {

        }

        private void btn_Tansform2FourGraphPane_Click(object sender, EventArgs e)
        {
            MasterPane masterPane = zgc1.MasterPane;

            masterPane.PaneList.Clear();
            // Set the master pane title
            //masterPane.Title.Text = "MasterPane Test";
            //masterPane.Title.IsVisible = true;

            // Fill the pane background with a color gradient
            masterPane.Fill = new Fill(Color.White, Color.MediumSlateBlue, 45.0F);

            // Set the margins and the space between panes to 10 points
            masterPane.Margin.All = 2;
            masterPane.InnerPaneGap = 0;

            ColorSymbolRotator rotator = new ColorSymbolRotator();

            for (int j = 0; j < 4; j++)
            {
                // Create a new graph with topLeft at (40,40) and size 600x400
                GraphPane myPaneT = new GraphPane(new Rectangle(40, 40, 600, 400),
                    "Case #" + (j + 1).ToString(),
                    "Time, Days",
                    "Rate, m/s");

                myPaneT.Fill.IsVisible = false;

                myPaneT.Chart.Fill = new Fill(Color.White, Color.LightYellow, 45.0F);
                myPaneT.BaseDimension = 3.0F;
                myPaneT.XAxis.Title.IsVisible = false;
                myPaneT.XAxis.Scale.IsVisible = false;
                myPaneT.Legend.IsVisible = false;
                myPaneT.Border.IsVisible = false;
                myPaneT.Title.IsVisible = false;
                myPaneT.XAxis.MajorTic.IsOutside = false;
                myPaneT.XAxis.MinorTic.IsOutside = false;
                myPaneT.XAxis.MajorGrid.IsVisible = true;
                myPaneT.XAxis.MinorGrid.IsVisible = true;
                myPaneT.Margin.All = 0;
                myPaneT.Margin.Top = 8;
                //if (j == 0)
                //    myPaneT.Margin.Top = 20;
                //if (j == 2)
                //{
                //    myPaneT.XAxis.Title.IsVisible = true;
                //    myPaneT.XAxis.Scale.IsVisible = true;
                //    myPaneT.Margin.Bottom = 10;
                //}

                //if (j > 0)
                //    myPaneT.YAxis.Scale.IsSkipLastLabel = true;

                // This sets the minimum amount of space for the left and right side, respectively
                // The reason for this is so that the ChartRect's all end up being the same size.
                myPaneT.YAxis.MinSpace = 60;
                myPaneT.Y2Axis.MinSpace = 20;

                // Make up some data arrays based on the Sine function
                double x, y;
                PointPairList list = new PointPairList();
                for (int i = 0; i < 36; i++)
                {
                    x = (double)i + 5 + j * 3;
                    y = 3.0 * (1.5 + Math.Sin((double)i * 0.2 + (double)j));
                    list.Add(x, y);
                }

                LineItem myCurve = myPaneT.AddCurve("Type " + j.ToString(),
                    list, rotator.NextColor, rotator.NextSymbol);
                myCurve.Symbol.Fill = new Fill(Color.White);

                masterPane.Add(myPaneT);
            }

            // Tell ZedGraph to auto layout all the panes
            using (Graphics g = zgc1.CreateGraphics())
            {
                masterPane.SetLayout(g, PaneLayout.SingleColumn);
                masterPane.AxisChange(g);
            }
            zgc1.AxisChange();
            zgc1.Invalidate();
        }

        private void btn_AddDynamicCurve_Click(object sender, EventArgs e)
        {
            int j = NextCurveIndex;
            if (pointListEdit[j].Count > 0)
            {
                pointListEdit[j].Clear();
            }

            Random random = new Random();
            double k = random.NextDouble();

            for (int i = 0; i < 36; i++)
            {
                double x = (double)i * k + 5;
                double y = 3.0 * (1.5 + Math.Sin((double)i * 0.2 + (double)k));
                pointListEdit[j].Add(x, y);
            }

            MasterPane masterPane = zgc1.MasterPane;
            GraphPane graphPane = masterPane.PaneList[0];
            graphPane.Legend.IsVisible = true;
            graphPane.AxisChange();
            zgc1.Invalidate();
        }

        private void btn_AddCurve_Click(object sender, System.EventArgs e)
        {
            GraphPane gp = zgc1.GraphPane;
            Random random = new Random();

            int index;
            Color color;
            while (true)
            {
                index = random.Next(0, 6);
                color = colors[index];
                if (usedIndex.Contains(index))
                {
                    LineItem lineItem = curveDic[color];
                    if (gp.CurveList.Remove(lineItem))
                    {
                        usedIndex.Remove(index);
                        gp.AxisChange();
                        zgc1.Invalidate();
                        continue;
                    }
                }
                else
                {
                    usedIndex.Add(index);
                    break;
                }
            }

            double x, y;
            PointPairList list = new PointPairList();
            bool isDown = false;
            for (ushort i = 95; ;)
            {
                x = (double)i + index * 10;
                y = x % 500 - index * 10;
                list.Add(x, y);
                if (i <= 805 && !isDown)
                {
                    if (i == 805)
                    {
                        isDown = true;
                    }
                    i += 5;
                }
                else
                {
                    if (i == 95)
                    {
                        break;
                    }
                    i -= 5;
                }
            }

            LineItem curve = gp.AddCurve($"Random {index}", list, color, SymbolType.None);
            curveDic.Add(color, curve);
            gp.AxisChange();
            zgc1.Invalidate();
        }
        #endregion 事件处理 end
    }
}